package com.controller;
import com.mapper.OssFileMapper;
import com.mapper.fileMapper;
import com.pojo.fileData;
import com.utils.OssUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pojo.OssFile;
import java.util.Map;


@RestController
@RequestMapping("/file")
public class UploadController {
    /**
     * 文件上传
     *
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @Autowired
    fileMapper filemapper;
    @Autowired
    OssFileMapper ossFileMapper;

    @PostMapping("/upload")
    @CrossOrigin
    public Map<String,Object> upload(HttpServletRequest request, @RequestParam("file") MultipartFile file,String open_id,String username) throws IOException {
            Map<String,Object> res=new HashMap<>();
            OssFile ossFile= OssUtils.uploadFile(file);
            ossFile.setOpen_id(open_id);
            ossFile.setUser_name(username);
            if(ossFile.getId()==null) ossFileMapper.upLoadFile(ossFile);//用来判断是否有重复文件的
            res.put("code",200);
            res.put("msg","文件上传成功");
            Map<String,String> data=new HashMap<>();
            data.put("url",ossFile.getOss_file_url());
            res.put("data",data);
            return  res;
    }


    @PostMapping("getFileByOpenId")
    @CrossOrigin
    public Map<String ,Object> getFileByOpenId(HttpServletRequest request, @RequestParam("open_id") String open_id) throws IOException {
        Map<String ,Object> res= new HashMap<>();
        List<OssFile> data=ossFileMapper.getFileByOpenId(open_id);
        res.put("code",200);
        res.put("data",data);
        return res;
    }


    @PostMapping("getFileById")
    @CrossOrigin
    public Map<String ,Object> getFileById(HttpServletRequest request, @RequestParam("id") String id) throws IOException {
        Map<String ,Object> res= new HashMap<>();
        res.put("code",200);
        OssFile data=ossFileMapper.getFileById(id);
        res.put("data",data);
        return res;
    }

    @PostMapping("/getFileList")
    @CrossOrigin
    public Map<String,Object> getFileList(HttpServletRequest request) {
        Map<String,Object> res=new HashMap<>();
        List<OssFile> data=ossFileMapper.getFileList();
        res.put("code",200);
        res.put("data",data);
        return res;
    }

    @PostMapping("/deleteFileById")
    @CrossOrigin
    public Map<String,Object> deleteFileById(HttpServletRequest request, @RequestParam("id") String id) throws IOException {
        Map<String,Object> res=new HashMap<>();
        String ans=ossFileMapper.deleteFileById(id);
        res.put("code",200);
        res.put("msg","删除成功!");
        return res;
    }




    @PostMapping("/examinetext")
    public Map<String,Object> textExamine() throws Exception{
        //文本格式审核
        String path = "E:/WorkPlace/毕设选题线上小程序/words/翁学森任务书.docx";

        Map<String, Object> wordFormat = auditWordFormat(path);

        return wordFormat;
    }

    public static Map<String,Object> auditWordFormat(String filePath) throws IOException {
        FileInputStream fis = new FileInputStream(new File(filePath));
        XWPFDocument document = new XWPFDocument(fis);

        Map<String,Object> text = new HashMap<>();
        // 检查文本格式
        int i = 1;
        for (XWPFParagraph paragraph : document.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                System.out.println("文本内容： " + run.getText(0));
                text.put("段落"+i+"文本内容：",run.getText(0));
                System.out.println("字体： " + run.getFontFamily());
                text.put("段落"+i+"字体：",run.getFontFamily());
                System.out.println("字号： " + run.getFontSize());
                text.put("段落"+i+"字号：",run.getFontSize());
                i++;
            }
        }

        // 检查表格格式
        int j = 1;
        for (XWPFTable table : document.getTables()) {
            for (XWPFTableRow row : table.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    for (XWPFParagraph paragraph : cell.getParagraphs()) {
                        for (XWPFRun run : paragraph.getRuns()) {
                            System.out.println("表格内容： " + run.getText(0));
                            text.put("表格"+j+"内容：",run.getText(0));
                            System.out.println("字体： " + run.getFontFamily());
                            text.put("表格"+j+"字体：",run.getFontFamily());
                            System.out.println("字号： " + run.getFontSize());
                            text.put("表格"+j+"字号：",run.getFontSize());
                            j++;
                        }
                    }
                }
            }
        }

        fis.close();

        return text;
    }

    @RequestMapping("/fileExamine")
    public Map<Object,Object> getfiledata(Integer id){
        int code = 200;
        String msg = "成功";

        System.out.println(id);
        fileData file = new fileData();

        //获取当前电脑时间
        Timestamp d = new Timestamp(System.currentTimeMillis());
        file.setReleaseTime(d);
        filemapper.updateCurrentTime(d);

        //获取学生姓名
        String studentname = filemapper.getStudentName(id);
        file.setSender(studentname);
        Integer studentid = filemapper.getStudentId(id);
        file.setId(studentid);

        //获取返回的文件地址
        String s = filemapper.getfileAddress(studentid);
        file.setAnnex(s);



        //新增进fileInf表
//        filemapper.insertStudentName(studentname);
//        filemapper.insertStudentId(studentid);

        List<fileData> list = new ArrayList<>();
        list.add(file);

        Map<Object,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("msg",msg);
        map.put("data",list);

//        filemapper.getfileData(id);
        System.out.println("完成");

        return map;
    }

    @Autowired
    private fileMapper nm;

    @RequestMapping("/getStudentFileList")
    public Map<String,Object> getList(Integer id,Integer role,Integer type,Integer page,Integer pagesize) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (page == null) page = 0;
        if (pagesize == null) pagesize = 10;
        if (type == null) {
            code = 500;
            msg = "type 不能为空";
        } else {
            page *= pagesize;
            if (type == 0) {
                List<fileData> list = nm.getAll(page, pagesize);
//                for (Notice item : list) {
//                    item.setSenderName(item.getType() == 0 ? nm.getAdminById(item.getSender()).getNickname() : nm.getTeacherById(item.getSender()).getName());
//                }
                map.put("data", list);
                map.put("total", nm.countAll());
            } else {
                if (id == null) {
                    code = 501;
                    msg = "id 不能为空";
                } else if (role == null) {
                    code = 502;
                    msg = "role 不能为空";
                } else {
                    List<fileData> list = nm.getList(id, role, page, pagesize);
//                    for (Notice item : list) {
//                        item.setSenderName(item.getType() == 0 ? nm.getAdminById(item.getSender()).getNickname() : nm.getTeacherById(item.getSender()).getName());
////                        item.setSenderName(nm.getTeacherById(item.getSender()).getName());
//                    }
                    map.put("data", list);
                    map.put("total", nm.countList(id, role));
                }
            }
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }
}


